//
//  SelectCountryVC.m
//  ImGuider
//
//  Created by llt on 2017/4/24.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "SelectCountryVC.h"
#import "CountryListDAO.h"

@interface SelectCountryVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *countryDict;
@property (nonatomic, strong) NSArray *keyArray;


@end

@implementation SelectCountryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)setup {
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kNavigationBarHeight)];
    topView.backgroundColor = kThemeColor;
    [self.view addSubview:topView];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backBtn.imageView.contentMode = UIViewContentModeLeft;
    [topView addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(topView).offset(20);
        make.left.equalTo(topView);
        make.bottom.equalTo(topView);
        make.width.mas_equalTo(44);
    }];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, SCREEN_WIDTH - 200, kNavigationBarHeight - 20)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = FontBold(17);
    [topView addSubview:titleLabel];
    
    titleLabel.text = MYLocalizedString(@"REGISTER_PLACE", nil);
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavigationBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT - kNavigationBarHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
    @weakify(self);
    
    if (![AppManager sharedManager].countryver || ![[AppManager sharedManager].version.cityver isEqualToString:[AppManager sharedManager].countryver]) {
        
        [ActivityIndicatorView showHUDAddedTo:self.view tintColor:[UIColor whiteColor]];
        [NetAPIManager getCountryListWithVersion:[AppManager sharedManager].version.cityver block:^(NSString *result, NSError *error) {
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            if (error) {
                return;
            }
            
            @strongify(self);
            [CountryListDAO getCountryBlock:^(NSArray *keyArray, NSDictionary *countryDict) {
                @strongify(self);
                self.keyArray = keyArray;
                self.countryDict = countryDict;
                [self.tableView reloadData];
            }];
        }];
        return;
    }
    
    [CountryListDAO getCountryBlock:^(NSArray *keyArray, NSDictionary *countryDict) {
        @strongify(self);
        self.keyArray = keyArray;
        self.countryDict = countryDict;
        [self.tableView reloadData];
    }];
    
    
}

- (void)cancelBtnClick {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark -UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.keyArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSArray *countryArray = self.countryDict[self.keyArray[section]];
    return countryArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"countryCell"];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"countryCell"];
        cell.textLabel.font = Font(12);
        cell.detailTextLabel.font = Font(11);
    }
    
    NSArray *countryArray = self.countryDict[self.keyArray[indexPath.section]];
    Country *country = countryArray[indexPath.row];
    
    cell.textLabel.text = country.smartName;
    cell.detailTextLabel.text = country.code;
    return cell;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    
    return self.keyArray[section];
}

- (nullable NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    
    return self.keyArray;
}

#pragma mark -UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSArray *countryArray = self.countryDict[self.keyArray[indexPath.section]];
    Country *country = countryArray[indexPath.row];
    [_delegate selectedCountry:country];
    [self cancelBtnClick];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
